<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="list" :props="{label:'name'}" default-expand-all>

        <template #default="{data}">
          <el-row style="width:100%;">
            <el-col :span="22">
              <div class="grid-content bg-purple-dark">{{ data.name }}</div>
            </el-col>
            <el-col :span="2">
              <div class="grid-content bg-purple-dark">
                <el-col :span="12"><div class="grid-content bg-purple-dark">{{ data.managerName || '暂无' }}</div></el-col>
                <el-col :span="12"><div class="grid-content bg-purple-dark">
                  <el-dropdown @command="handleClick($event, data.id)">
                    <span class="el-dropdown-link">
                      操作<i class="el-icon-arrow-down el-icon--right" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item command="add">添加子菜单</el-dropdown-item>
                      <el-dropdown-item command="edit">编辑子菜单</el-dropdown-item>
                      <el-dropdown-item command="del">删除子菜单</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </div>
                </el-col>
              </div>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <add-dept ref="addDept" :show-dialog.sync="showdialog" :source-dets="sourceDets" :pid="pid" @reload="getDepts" />
  </div>
</template>
<script>
import { reqGetDepts, reqDelDept } from '@/api/department'
import AddDept from './components/add-dept.vue'
// 引入处理树形结构的方法
import { translatetoTreeDate } from '@/utils/index'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      showdialog: false,
      list: [],
      sourceDets: [],
      pid: -1
    }
  },
  created() {
    this.getDepts()
  },
  methods: {
    // 发起请求，
    async getDepts() {
      const res = await reqGetDepts()
      // console.log(res.data)
      this.sourceDets = res.data
      // 调用处理树形结构的方法
      this.list = translatetoTreeDate(res.data, 0)
    },
    // 使用element组件进行点击打开对话框
    async handleClick(command, id) {
      // console.log(id)
      // 接收id
      this.pid = id
      // console.log(command)
      if (command === 'add') {
        // console.log('添加')
        // 修改编辑
        this.$refs.addDept.title = '添加部门'

        this.showdialog = true
      }
      if (command === 'edit') {
        console.log('编辑')
        this.$refs.addDept.title = '编辑部门'
        // this.pid = id
        // 由于是异步的，需要使用$nextTick()来使用
        this.$nextTick(() => {
          this.$refs.addDept.showEditDept()
        })
        // 另一种方法 通过子组件方法传值
        // this.$refs.addDept.showEditDept(id)

        this.showdialog = true
      }
      // 删除
      if (command === 'del') {
        // 确认消息
        const str = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(() => {})
        // console.log('删除')
        console.log(str)
        if (str === 'confirm') {
          const res = await reqDelDept(id)
          console.log(res)
          this.getDepts()
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  padding: 50px 200px;
}
</style>
